package com.ruoyi.web.controller.system;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.ruoyi.common.core.domain.entity.CommonEntity;
import com.ruoyi.common.core.page.PageQuery;
import com.ruoyi.common.core.page.PageUtil;
import com.ruoyi.common.core.page.ResultEntity;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.system.domain.MaterialReportEntity;
import com.ruoyi.system.service.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.var;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.time.temporal.ChronoUnit;
import java.util.Date;


@Api(tags = "物料管理")
@RestController
@RequestMapping("/material")
public class MaterialReportController {


    @Autowired
    private MaterialReportService materialReportService;


    @PostMapping("/report/add")
    @ApiOperation(value = "新增物料验收检验批次")

    /*
     *   1. 判断 report 是否 已经检测过检测过 直接返回错误  已经检测过
     *   2. 新增或者修改 report
     *   3. 针对入库记录 添加检测报告
     * */
    public ResultEntity addReport(@RequestBody MaterialReportEntity params){

        params.setCreateBy(SecurityUtils.getLoginUser().getUser().getId());
        params.setUpdateBy(SecurityUtils.getLoginUser().getUser().getId());
        params.setCreator(SecurityUtils.getLoginUser().getUser().getName());
        params.setCreateTime(new Date());
        params.setUpdateTime(new Date());

        materialReportService.upsertMaterialLogReport(params);
        return ResultEntity.createSuccess();
    }

    @GetMapping("/font/report/list")
    @ApiOperation(value = "首页的验收批次1列表")
    public ResultEntity fontListReport(){
        var nowTimeFormat =  LocalDateTime.now();

        LocalDateTime halfMonthAgo = nowTimeFormat.minus(30, ChronoUnit.DAYS);
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");

        // 格式化日期时间
        String formattedDateTime = halfMonthAgo.format(formatter);
        return ResultEntity.createSuccess(materialReportService.list(new LambdaQueryWrapper<MaterialReportEntity>()
                .eq(MaterialReportEntity::getDelFlag,0).ge(MaterialReportEntity::getCreateTime, formattedDateTime)
                .orderByDesc(CommonEntity::getUpdateTime)));
    }

    @GetMapping("/report/list")
    @ApiOperation(value = "物料验收检验批次列表")
    public ResultEntity listReport(PageQuery pageQuery){
        PageUtil.startPage(pageQuery);
        return ResultEntity.createSuccess(PageUtil.getPageList(materialReportService.list(new LambdaQueryWrapper<MaterialReportEntity>()
                .eq(MaterialReportEntity::getDelFlag,0)
                .orderByDesc(CommonEntity::getUpdateTime))));
    }
    /*
     * */
    @GetMapping("/report/delete")
    @ApiOperation(value = "删除新增物料验收检验批次")
    public ResultEntity deleteReport(@RequestParam("id") Long id){
        MaterialReportEntity entity = new MaterialReportEntity();
        entity.setId(id);
        entity.setDelFlag(1);
        materialReportService.updateById(entity);
        return ResultEntity.createSuccess();
    }
}
